package day9;

import java.util.Random;

/**
 * @author oldliu
 * @since 1.0
 */
public class MoneyTaker_old extends Thread {
    private final Accout ac;

    public MoneyTaker_old(String name, Accout ac) {
        super(name);
        this.ac = ac;
    }

    @Override
    public void run() {
        Random r = new Random();
        while (ac.getMoney() > 0) {
            int money = r.nextInt(200);//0-200
            System.out.println(getName() + "计划取钱：" + money);
            //锁代码段的方案
            synchronized (ac) {
                if (ac.getMoney() >= money) {
                    try {
                        sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ac.takeAway(money);
                    System.out.println(getName() + "取钱：" + money + "成功，现在余额是：" + ac.getMoney() + "元");
                } else {
                    System.out.println("ATM系统告诉" + getName() + "：余额不足了，现在还有" + ac.getMoney() + "元");
                }
            }
        }
        System.out.println("账户余额是：" + ac.getMoney() + "元");
    }
}
